#include "../include/vfshash.h"
#include "../include/vfslinkedlist.h"
#include<stdio.h>
#include<malloc.h>


//Returning index of the hash table
int hash_value(char var){

	if(var >= '0' && var <= '9')
	{
		return var - '0';
	}
	
	else if(var >= 'A' && var <= 'Z')
	{
		return var - 'A' + 10;
	}
	
	else if(var >= 'a' && var <= 'z')
	{
		return var - 'a' + 10 + 26;
	}
}


//Insertion into hash table
hash *hash_insert(hash *ht_start, char filename[])
{
	char var;
	int index;
	
	var = filename[0];
	index = hash_value(var);
	
	ht_start->htable[index] = insert(ht_start->htable[index], filename);
	
	return ht_start;
}



//Initializeing all the buckets of hash table to NULL
 void hash_initialize(hash *ht)
{
	int i,flag=1;
	if(ht == NULL) flag=0;
	//return;
	for(i = 0;(i<62 && flag);i++)
	{
	ht->htable[i] = NULL;
	}
}



//Displaying the contents of hash table
void display_hashTable(hash *ht_start)
{
	int i;
	for(i=0;i<62;i++)
	{
		display(ht_start->htable[i]);
		printf("\n");
	}
}



//Search the hash table for a particular filename
l_nodeSearch *search_hashTable(hash *ht_start, char filename[])
{

	char var;
	int index;
	l_nodeSearch *searchStart;
	
	var = filename[0];
	index = hash_value(var);
	
	searchStart = SearchByStringKey(ht_start->htable[index], filename);
	
	return searchStart;
}



//Deleting from hash table
void deleteFromHashTable(hash *ht_start, char filename[])
{
	l_node *deletedStart;
	char var;
	int index;
	l_nodeSearch *searchStart;
	
	var = filename[0];
	index = hash_value(var);
	deletedStart = deleteFromLinkedlist(ht_start->htable[index], filename);
	ht_start->htable[index] = deletedStart;
	
	//return deletedStart;
}
